#include <iostream>
#include <ctime>
#include <windows.h>
#include <stack>

using namespace std;

#define _MAX 99              //参与运算的最大数字
#define _isSimpleMulti ture  //是否开启简单乘法


//运算符
char AllP[] = {'+', '-', '*', '/'};
char PartP[] = {'+', '-', '*'};
//运算器
stack<int> s1;
stack<char> s2;

//获得运算符
char getP(int &a, int &b, char prec) {
    int beDevided;
    char op;
    if (prec == '/')op = PartP[rand() % 3];
    else op = AllP[rand() % 4];
    if (_isSimpleMulti)if (op == '*')b /= 10;//减小乘数，使乘法简单
    else if (op == '/') {
        beDevided = (rand() % 10 + 1);
        a = beDevided * b;//使除法拥有整数结果
    }
    return op;
}

//用于判断运算符优先级的高低，优先级高于或等于就返回true，否则为false
bool pri(char a, char b) {
    return ((a == '+' || a == '-') && (b == '+' || b == '-' || b == '*' || b == '/')) ||
           ((a == '*' || a == '/') && (b == '*' || b == '/'));
}

//计算
void calc() {
    int a1, a2;
    char c;
    a1 = s1.top();
    s1.pop();
    a2 = s1.top();
    s1.pop();
    c = s2.top();
    s2.pop();
    switch (c) {
        case '+':
            a2 += a1;
            break;
        case '-':
            a2 -= a1;
            break;
        case '*':
            a2 *= a1;
            break;
        case '/':
            a2 /= a1;
    }
    s1.push(a2);
}

int main() {
    int ans, exit, n, i, count, right;
    bool firstBlood;
    //获取计算量
    do {
        cout << "cin a num:";
        cin >> n;
        if (cin.fail()) {
            cin.clear();
            cin.ignore(10000, '\n');
        }
    } while (n < 2);
    int *a = new int[n];
    char *p = new char[n - 1];
    count = 0;
    right = 0;
    /***********************************************/
    while (true) {
        firstBlood = true;
        count++;
        ans = 0;
        exit = 0;
        //获得随机数
        srand(time(0));
        for (i = 0; i < n; i++)
            a[i] = rand() % (_MAX) + 1;
        //获得随机运算符
        p[0] = getP(a[0], a[1], '+');
        for (i = 1; i < n - 1; i++)
            p[i] = getP(a[i], a[i + 1], p[i - 1]);
        /***********************************************/
        L:
        system("cls");
        //两次非数字输入以退出
        if (exit == 1)cout << "Do it again to exit.\n";
        else if (exit == 2)return 0;
        //生成算式
        cout << "[" << right << "/" << count << "] " << a[0] << " ";
        for (i = 0; i < n - 1; i++)
            cout << p[i] << " " << a[i + 1] << " ";
        cout << "= ";
        //处理输入
        cin >> ans;
        if (cin.fail()) {
            exit++;
            cin.clear();
            cin.ignore(10000, '\n');
            goto L;
        }
        /*运算*/
        s1.push(a[0]);
        s2.push(p[0]);
        for (i = 1; i < n - 1; i++) {
            s1.push(a[i]);
            while (!s2.empty() && pri(p[i], s2.top())) {
                calc();
            }
            s2.push(p[i]);
        }
        s1.push(a[n - 1]);
        while (!s2.empty())calc();
        i = s1.top();
        s1.pop();
        //判断结果
        if (i == ans) {
            system("cls");
            if (firstBlood)right++;
            //更新得分
            cout << "[" << right << "/" << count << "] " << a[0] << " ";
            for (i = 0; i < n - 1; i++)
                cout << p[i] << " " << a[i + 1] << " ";
            cout << "= " << ans;
            if (firstBlood)
                cout << "  Excellent!\n+1";
            else cout << "  Right.";
            Sleep(800);
        } else {
            cout << "Wrong:" << i;
            firstBlood = false;
            Sleep(800);
            goto L;
        }
    }
}
